package com.cskaoyan.controller.promotion;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.CommonData;
import com.cskaoyan.bean.bo.promotionbo.AdminBo;
import com.cskaoyan.bean.bo.promotionbo.MarketCouponBo;
import com.cskaoyan.bean.po.promotionpo.MarketCoupon;
import com.cskaoyan.bean.po.promotionpo.MarketCouponUser;
import com.cskaoyan.exception.MarketValidationException;
import com.cskaoyan.service.promotion.AdminCouponService;
import com.cskaoyan.util.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Zhang Tao
 * @version 1.0.0
 * @description
 * @date since 2022-07-16 21:33
 */
@RestController
@RequestMapping("admin/coupon")
public class AdminCouponController {
    @Autowired
    AdminCouponService adminCouponService;


    @GetMapping("list")
    public BaseRespVo adminCouponList(AdminBo adminBo) {
        CommonData<MarketCoupon> couponList = null;
        try {
            couponList = adminCouponService.getCoupons(adminBo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(couponList);
    }

    @PostMapping("create")
    public BaseRespVo createAdminCoupon( @Validated @RequestBody MarketCouponBo marketCouponBo,BindingResult result) throws MarketValidationException {
        ValidationUtils.valid(result);
        try {
            if(marketCouponBo.getDiscount().compareTo(marketCouponBo.getMin())>0){
                int i =1/0;
            }
        } catch (ArithmeticException e) {
            throw new ArithmeticException("满减金额需大于优惠金额");
        }

        try {
            if(marketCouponBo.getEndTime().before(marketCouponBo.getStartTime())){
                int i =1/0;
            }
        } catch (ArithmeticException e) {
            throw new ArithmeticException("领券日期不符合规范");
        }
        MarketCoupon marketCoupon1 = null;
        try {
            marketCoupon1 = adminCouponService.createAdminCoupon(marketCouponBo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(marketCoupon1);
    }

    @GetMapping("read")
    public BaseRespVo readAdminCoupon(Integer id) {
        MarketCoupon marketCoupon = null;
        try {
            marketCoupon = adminCouponService.readAdminCoupons(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(marketCoupon);
    }

    @PostMapping("update")
    public BaseRespVo updateAdminCoupon(@Validated @RequestBody MarketCoupon marketCoupon,BindingResult result) throws MarketValidationException {
        ValidationUtils.valid(result);
        try {
            if(marketCoupon.getDiscount().compareTo(marketCoupon.getMin())>0){
                int i =1/0;
            }
        } catch (ArithmeticException e) {
            throw new ArithmeticException("满减金额需大于优惠金额");
        }

        try {
            if(marketCoupon.getEndTime().before(marketCoupon.getStartTime())){
                int i =1/0;
            }
        } catch (ArithmeticException e) {
            throw new ArithmeticException("领券日期不符合规范");
        }
        MarketCoupon marketCoupon1 = null;
        try {
            marketCoupon1 = adminCouponService.updateAdminCoupon(marketCoupon);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(marketCoupon1);
    }

    @PostMapping("delete")
    public BaseRespVo deleteAdminCoupon(@RequestBody MarketCoupon marketCoupon) {
        try {
            adminCouponService.deleteAdminCoupons(marketCoupon);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(null);
    }

    @GetMapping("listuser")
    public BaseRespVo getCouponListUser(AdminBo adminBo) {
        CommonData<MarketCouponUser> couponUserList = null;
        try {
            couponUserList = adminCouponService.getCouponListUser(adminBo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return BaseRespVo.ok(couponUserList);
    }
}

